package com.tc.quartz.support.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tc.quartz.frame.base.BaseController;

import com.tc.quartz.frame.bean.RespBean;
import com.tc.quartz.frame.common.ParamUtil;
import com.tc.quartz.support.bean.SystemDictBean;
import com.tc.quartz.support.service.ISystemDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *dict管理
 * @author yongjin.ye
 *
 */
@RequestMapping(value = "/systemDict")
@Controller
public class SystemDictController extends BaseController {

	@Autowired
	private ISystemDictService systemDictService;
	
	/**
	 * 进入
	 */
	@RequestMapping(value="/list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public String list() {
		return "forward:/jsp/systemDict/list.jsp";
	}
	
	
	/**
	 * 查询 
	 */
	@RequestMapping(value="/list", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String listQuery() {
		int page = ParamUtil.getInt(this.getRequest(), "page", 1);
		int pagesize = ParamUtil.getInt(this.getRequest(), "pagesize", 10);
		PageInfo<SystemDictBean> pageInfo = this.systemDictService.getPageList(null, page, pagesize);
		return JSON.toJSONString(pageInfo);
	}
	
	
	/**
	 * 保存
	 */
	
	@RequestMapping(value="/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String add(SystemDictBean requser) {
		this.systemDictService.insert(requser);
		RespBean bean = new RespBean();
		bean.setCode("SUCCESS");
		bean.setMessage("操作成功！");
		return bean.toJson();
	}
	
	/**
	 * 更新
	 */
	
	@RequestMapping(value="/update", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String update(SystemDictBean requser) {
		this.systemDictService.update(requser);
		RespBean bean = new RespBean();
		bean.setCode("SUCCESS");
		bean.setMessage("操作成功！");
		return bean.toJson();
	}
	
	
	/**
	 * delete
	 */
	@RequestMapping(value="/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String delete(SystemDictBean requser) {
		RespBean bean = new RespBean();
		SystemDictBean uEntity= (SystemDictBean) this.systemDictService.select(requser).get(0);
		if (uEntity.getDictType().equals("system")) {
			bean.setCode("SUCCESS");
			bean.setMessage("系统字典不允许删除");
		} else {
			this.systemDictService.delete(uEntity);
			bean.setCode("SUCCESS");
			bean.setMessage("操作成功！");
		}
		return bean.toJson();
	}
	
	
	/**
	 * 更新缓存
	 */
	
	@RequestMapping(value="/refreshCache", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String refreshCache() {
		this.systemDictService.getDictCache().clear();
		RespBean bean = new RespBean();
		bean.setCode("SUCCESS");
		bean.setMessage("更新缓存成功！");
		return bean.toJson();
	}
	
}